package com.mi.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mi.bean.CustomersBean;
import com.mi.service.impl.LoginServiceImpl;
import com.mi.service.impl.UpdateServiceImpl;
import com.mi.utils.MyUtils;

/**
 * Servlet implementation class CustomerUpdateAction
 */
@WebServlet("/CustomerUpdateAction")
@MultipartConfig(maxFileSize=1024*1024*5) // 计算的基本单位是字节（byte）用于上传文件
public class CustomerUpdateAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 用户修改信息
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 将表单信息存储到实体类中
		CustomersBean customerB = MyUtils.convertFormData2Bean(request, CustomersBean.class);
		
		System.out.println("action-form表单转化成的实体类"+customerB.toString());
		// 获取用户Id
		String customerId = (String) request.getSession().getAttribute("sid");

		/*System.out.println("action-从session中获取的用户Id:" + customerId);*/

		int i = MyUtils.getInstance(UpdateServiceImpl.class).updateUserInfo(customerB, customerId);

		if (i > 0) {
			// 修改成功 返回本页
			
			String strName = customerB.getCustomer_name();
			String strTel = customerB.getCustomer_tel();
			String strEmail = customerB.getCustomer_email();
			String strImg = customerB.getCustomer_img();
			
			/*System.out.println("action-修改后的用户信息"+customerB.toString());*/
			
			// 判断用户信息是否被修改
			if(strName != null && strName.length() > 0) {
				// 用户名被修改，重新存入到会话中
				request.getSession().setAttribute("sname", strName); // session设值
			}
			if(strTel != null && strTel.length() > 0) {
				// 用户手机号被修改，重新存入到会话中
				request.getSession().setAttribute("stel", strTel);
			}
			if(strEmail != null && strEmail.length() > 0) {
				// 用户邮箱被修改，重新存入到会话中
				request.getSession().setAttribute("semail", strEmail);
			}
			if(strImg != null && strImg.length() > 0) {
				// 用户头像被修改，重新存入到会话中
				request.getSession().setAttribute("simg", strImg);
			}
			// 安全目录下要使用转发来跳转页面
			request.getRequestDispatcher("userinfo.jsp").forward(request, response);

		}else {
			request.getRequestDispatcher("userinfo.jsp").forward(request, response);
		}
	}

}
